<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleleaf.org">
<head th:insert="base">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" action="" method="post" lay-filter = "editObj">
            <input type="hidden" name="id"/>
            <div class="layui-form-item">
                <label for="nickName" class="layui-form-label">
                    用户名
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="nickName" name="nickName" minlength="3" maxlength="20" lay-verify="range"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="userName" class="layui-form-label">
                    姓名
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="userName" name="userName" minlength="3" maxlength="20" lay-verify="range"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="userSex" class="layui-form-label">
                    性别
                </label>
                <div class="layui-input-inline">
                    <select id="userSex" name="userSex" class="valid" lay-verify="required">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="userEmail" class="layui-form-label">
                    用户邮箱
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="userEmail" name="userEmail" lay-verify="required|email"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="userTel" class="layui-form-label">
                    用户电话
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="userTel" name="userTel" lay-verify="required|phone"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="areaCode" class="layui-form-label">
                    区号
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="areaCode" name="areaCode" minlength="3" maxlength="20" lay-verify="range"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="roleCode" class="layui-form-label">
                    选择角色
                </label>
                <div class="layui-input-inline">
                    <select id="roleCode" name="roleCode" class="valid" lay-verify="required" lay-filter="roleName">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
                <input type="hidden" id="roleName" name="roleName"/>
            </div>
            <div class="layui-form-item layui-form-text">
                <label for="remark" class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" id="remark" name="remark" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                </label>
                <button  class="layui-btn" lay-filter="edit" lay-submit="">
                    确认修改
                </button>
            </div>
        </form>
    </div>
</div>
<script th:inline="javascript">
    layui.use(['form','jquery','enums','validate','tool','roleUtil'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var enums = layui.enums;
        var validate = layui.validate;
        var baseUrl = "/back/admin";
        var tool = layui.tool;
        var roleUtil = layui.roleUtil;
        var userInfo = [[${userInfo}]];
        init_form();

        /**
         * 初始化页面
         */
        function init_form() {
            enums.createSelectByEnum('userSex','EnumUserSex',null,null,'请选择性别');
            roleUtil.createRoleCodeSelect('roleCode',null,null,'请选择角色');
            form.render();//重新渲染
            form.val('editObj',{
                "id":userInfo.id
                ,"nickName":userInfo.nickName
                ,"userName":userInfo.userName
                ,"userSex":userInfo.userSex
                ,"userEmail":userInfo.userEmail
                ,"userTel":userInfo.userTel
                ,"areaCode":userInfo.areaCode
                ,"roleCode":userInfo.roleCode
                ,"roleName":userInfo.roleName
                ,"remark":userInfo.remark
            });
        }

        //添加表单校验
        form.verify(validate);

        form.on('select(roleName)', function (data) {
            $("#roleName").val(roleUtil.getRoleName(data.value));
            console.log(roleUtil.getRoleName(data.value));
        });

        //监听提交
        form.on('submit(edit)', function(data){
            var res = tool.submit(baseUrl + '/edit',data.field);
            if(res.code == return_200){
                layer.msg(res.msg, {
                        icon: msg.success,
                        time: 2000
                    },
                    function(){
                        xadmin.father_reload();
                    }
                );
            }else {
                layer.msg(res.msg, {
                        icon: msg.error,
                        time: 2000
                    },
                    function(){
                        xadmin.father_reload();
                    }
                );
            }
            return false;
        });
    });
</script>
</body>
</html>